﻿using System.Drawing;
using Lidgren.Network;

namespace Model.LiveControl
{
    public struct Rect 
    {
        public int Left; //最左坐标
        public int Top; //最上坐标
        public int Right; //最右坐标
        public int Bottom; //最下坐标

        public void Union(Rect other)
        {
            if (other.Left >= other.Right || other.Top >= other.Bottom )
                return;
            Left = Left < other.Left ? Left : other.Left;
            Top = Top < other.Top ? Top : other.Top;
            Right = Right > other.Right ? Right : other.Right;
            Bottom = Bottom > other.Bottom ? Bottom : other.Bottom;
        }
        public Rectangle ToRectangle()
        {
            return new Rectangle(Left, Top, Right - Left, Bottom - Top);
        }
    }

    public class IdRect : ISerializable
    {
        
        public int Ptr =0; //句柄
        public Rect Rect = new Rect();

        public void WritePayload(NetOutgoingMessage message)
        {
            message.Write(Ptr);
            message.Write(Rect.Left);
            message.Write(Rect.Top);
            message.Write(Rect.Right);
            message.Write(Rect.Bottom);
        }

        public void ReadPayload(NetIncomingMessage message)
        {
            Ptr = message.ReadInt32();
            Rect.Left = message.ReadInt32();
            Rect.Top = message.ReadInt32();
            Rect.Right = message.ReadInt32();
            Rect.Bottom = message.ReadInt32();
        }

       
    }
    public class RectList : ISerializable
    {
        public IdRect[] Rects;

        public void WritePayload(NetOutgoingMessage message)
        {
            message.Write(Rects.Length);
            foreach (var rect in Rects)
            {
                rect.WritePayload(message);
            }
        }

        public void ReadPayload(NetIncomingMessage message)
        {
            var count = message.ReadInt32();
            Rects = new IdRect[count];
            for (var i = 0; i < count; i++)
            {
                IdRect tRect= new IdRect();
                tRect.ReadPayload(message);
                Rects[i] = tRect;
            }
        }
    }
}